#!/usr/bin/env bash
# Initialize the host connection to allow all connections to datanodes and authenticated connections to coordinators.

CUR_PATH="$(dirname "${BASH_SOURCE[0]}")"
# shellcheck source=./common.sh
source "${CUR_PATH}/common.sh"

export PG_AUTH_METHOD="trust"
if [ -n "${PGPASSWORD}" ] && [ -n "${AUTH_TYPE}" ]; then
  PG_AUTH_METHOD="${AUTH_TYPE}"
  log:info "Added network config to allow password protected connections."
else
  log:warning "Superuser password not found, all connections will be allowed!"
fi

if [ "${NODE_TYPE}" == "datanode" ]; then
  echo "host all all all trust" >>"${PGDATA}/pg_hba.conf"
else
  echo "host all all all ${PG_AUTH_METHOD}" >>"${PGDATA}/pg_hba.conf"
fi
echo "host replication all all ${PG_AUTH_METHOD}" >>"${PGDATA}/pg_hba.conf"
